//
//  ChatServer.m
//  Bees
//
//  Created by NowYouCanSee on 2019/3/4.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "ChatServer.h"

@implementation ChatServer

- (void)getChatroomListIsMore:(BOOL)isMore completion:(void(^)(BOOL success, BOOL isHaveNextPage,NSString *msg))completion{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSInteger page = isMore ? (self.cmtPage + 1) : 1;
    params[@"page"]=@(page);
    self.latestParams = params;
    [NetworkManager GET:URL_chat_room parameters:params success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            XHBasePagingModel *pageM = [XHBasePagingModel modelWithJSON:base.data];
            // 用户上拉后有快速下拉, 下拉的数据先回来, 上拉的数据后回来
            if (self.latestParams != params) {
                return;
            }
            //记录页码
            self.cmtPage = page;
            if (!isMore) {
                [self.chatList removeAllObjects];
            }
            [self.chatList addObjectsFromArray:[NSArray modelArrayWithClass:[ChatroomModel class] json:pageM.data]];
            !completion?:completion(YES,pageM.isHaveNextPage,base.message);
        }else{
            !completion?:completion(NO,NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,NO,kNetworkError);
    }];
}

- (void)getMyChatroomListCompletion:(void(^)(BOOL success,NSString *msg))completion{
    [NetworkManager GET:URL_chat_room_my parameters:@{}success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            self.myChatList= [NSArray modelArrayWithClass:[ChatroomModel class] json:base.data];
            !completion?:completion(YES,base.message);
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)getSearchChatroomListIsMore:(BOOL)isMore completion:(void(^)(BOOL success, BOOL isHaveNextPage,NSString *msg))completion{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSInteger page = isMore ? (self.cmtPage + 1) : 1;
    params[@"page"]=@(page);
    params[@"key"]= VALIDATE_STRING(self.searchKey);
    self.latestParams = params;
    [NetworkManager GET:URL_chat_room_search parameters:params success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
            XHBasePagingModel *pageM = [XHBasePagingModel modelWithJSON:base.data];
            // 用户上拉后有快速下拉, 下拉的数据先回来, 上拉的数据后回来
            if (self.latestParams != params) {
                return;
            }
            //记录页码
            self.cmtPage = page;
            if (!isMore) {
                [self.chatList removeAllObjects];
            }
            [self.chatList addObjectsFromArray:[NSArray modelArrayWithClass:[ChatroomModel class] json:pageM.data]];
            !completion?:completion(YES,pageM.isHaveNextPage,base.message);
        }else{
            !completion?:completion(NO,NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,NO,kNetworkError);
    }];
}

- (void)joinChatroom:(NSString*)roomId completion:(void(^)(BOOL success,NSString *msg))completion{
    [NetworkManager POST:URL_chat_room parameters:@{@"room_id":VALIDATE_STRING(roomId)} success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
//            [[NIMSDK sharedSDK].teamManager applyToTeam:roomId message:@"" completion:^(NSError * _Nullable error, NIMTeamApplyStatus applyStatus) {
//                if (!error ) {
                    !completion?:completion(YES,base.message);
//                }else{
//                    !completion?:completion(NO,error.description);
//                }
//            }];
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);
    }];
}
- (void)deleteChatroom:(NSString*)roomId completion:(void(^)(BOOL success,NSString *msg))completion{
    [NetworkManager DELETE:URL_chat_room parameters:@{@"room_id":VALIDATE_STRING(roomId)} success:^(id responseObject) {
        XHBaseModel *base = [XHBaseModel modelWithJSON:responseObject];
        if ([base.code isEqualToNumber:@200]) {
//            [[NIMSDK sharedSDK].teamManager quitTeam:roomId  completion:^(NSError *error) {
//                if (!error) {
                    !completion?:completion(YES,base.message);
//                }else{
//                    !completion?:completion(NO,[NSString stringWithFormat:@"退出失败 code:%zd",error.code]);
//                }
//            }];
        }else{
            !completion?:completion(NO,base.message);
        }
    } failure:^(NSError *error) {
        !completion?:completion(NO,kNetworkError);
    }];
}
- (NSMutableArray<ChatroomModel*>*)chatList{
    if (!_chatList) {
        _chatList = [NSMutableArray array];
    }
    return _chatList;
}

@end
